import { useCourseStoreHook } from "@/store/modules/course";
import { useStudyStoreHook } from "@/store/modules/study";
import { translateRouteTitleI18n } from "@/utils/i18n";

export const setDocumentTitle = function (to: any) {
  if (to.meta.layout === "CourseLayout" || to.meta.layout === "CorrectLayout") {
    const courseStore = useCourseStoreHook();
    document.title =
      translateRouteTitleI18n("我教的课") +
      "_" +
      courseStore.courseName +
      "_" +
      translateRouteTitleI18n(to.meta.title);
  } else if (to.meta.layout === "StudyLayout") {
    const studyStore = useStudyStoreHook();
    document.title =
      translateRouteTitleI18n("我学的课") +
      "_" +
      studyStore.courseName +
      "_" +
      translateRouteTitleI18n(to.meta.title);
  } else {
    if (to.meta.title === "dashboard") {
      document.title = translateRouteTitleI18n("首页");
    } else {
      document.title =
        translateRouteTitleI18n("首页") +
        "_" +
        translateRouteTitleI18n(to.meta.title);
    }
  }

  const ua = navigator.userAgent;
  const regex = /\bMicroMessenger\/([\d\.]+)/;
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement("iframe");
    i.src = "/favicon.ico";
    i.style.display = "none";
    i.onload = function () {
      setTimeout(function () {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
};
